<?php
/**
 * 加密代码库集
 * 
 * $Id: crypt.functions.php 122 2010-05-28 10:05:16Z Administrator $
 * $Revision: 122 $
 * $Author: Administrator $
 * $Date: 2010-05-28 18:05:16 +0800 (星期五, 28 五月 2010) $
 * 本模块是一些常用的加密算法。本模块中的算法很有用
 * 但是php中往往没有提供。我们在不断的增加更多的算法
 * 
 * @author glzaboy<glzaboy@163.com>
 * @package fastlib
 * @subpackage functions
 * @copyright 2005-2010 (c) glzaboy
 */
if (! defined ( '__FASTLIB__' )) {
	if (__DEBUG__) {
		die ( sprintf ( 'Forbidden 403 at file %s on line %d', __FILE__, __LINE__ ) );
	} else {
		die ( 'Forbidden 403' );
	}
}
/**
 * 设置页面CSS,JS,RSS,FETCH,INCLUDE代码
 *
 * @param Array $Rule 要设置的代码信息比如CSS　
 * setRule(array('src'=>"./view/{$_COOKIE['SKIN']}/css/login.css"),'css','a');
 * @param String $type 类型有 JS,CSS,RSS,FETCH,INCLUDE
 * @param String $method 操作类型 A(结尾),I(前面),R(取代),C(清除)
 * @return true
 */
/**
 * Rc4加密算法
 *
 * @param String $string 加密码的内容
 * @param string $key 密钥
 * @param int $skip 向量
 * @return string 加密后的结果 
 */
function cryptCr4($string, $key = '', $skip = 0) {
	$key_length = strlen ( $key );
	$string_length = strlen ( $string );
	$rndkey = array ();
	for($i = 0; $i <= 255; $i ++) {
		$rndkey [$i] = ord ( $key [$i % $key_length] );
	}
	$box = range ( 0, 255 );
	for($j = $i = 0; $i < 256; $i ++) {
		$j = ($j + $box [$i] + $rndkey [$i]) % 256;
		$tmp = $box [$i];
		$box [$i] = $box [$j];
		$box [$j] = $tmp;
	}
	for($a = $j = $i = 0; $i < $skip; $i ++) {
		$a = ($a + 1) % 256;
		$j = ($j + $box [$a]) % 256;
		$tmp = $box [$a];
		$box [$a] = $box [$j];
		$box [$j] = $tmp;
	}
	for($a = $j = $i = 0; $i < $string_length; $i ++) {
		$a = ($a + 1) % 256;
		$j = ($j + $box [$a]) % 256;
		$tmp = $box [$a];
		$box [$a] = $box [$j];
		$box [$j] = $tmp;
		$result .= chr ( ord ( $string [$i] ) ^ ($box [($box [$a] + $box [$j]) % 256]) );
	}
	return $result;
}
/**
 * 返回本模块信息
 *
 * @return string
 */
function cryptprintInfo() {
	$version = '$Revision: 122 $';
	$moudle = explode ( "functions", __FILE__ );
	$moudle = $moudle [1] ? substr ( $moudle [1], 1 ) . 'functions' : $moudle;
	$return = '';
	$return .= startTable ();
	$return .= printRow ( array (array ('class' => 'h', 'content' => '<center>' . $moudle . '</center>', 'colspan' => '2' ) ) );
	$return .= printRow ( array (array ('class' => 'e', 'content' => 'author' ), array ('class' => 'v', 'content' => 'glzaboy' ) ) );
	$return .= printRow ( array (array ('class' => 'e', 'content' => 'version' ), array ('class' => 'v', 'content' => $version ) ) );
	$return .= printRow ( array (array ('class' => 'e', 'content' => 'support' ), array ('class' => 'v', 'content' => 'rc4' ) ) );
	$return .= endTable ();
	return $return;
}